<?php 
require_once ("classes/mysql.class.php");
require_once ("classes/catalog.class.php");

function buildTable($page, $order) {
    $catalog = Catalog::getInstance();
    $collection = $catalog->getListFile($page, $order);
    
    $html = "";
    foreach ($collection as $record) {
        $html .= sprintf("<tr id=\"file:%s\" fileid=\"%s\" class=\"status-%s\" status=\"%s\">", $record["id"], $record["id"], $record["status"], $record["status"]);
        $html .= sprintf("<td class=\"checks\"><div><input type=\"checkbox\" name=\"multicheck\" value=\"%s\" /></div></td><td class=\"id\"><div>%s</div></td><td class=\"filename truncate\"><div><span>%s</span><span>%s</span></div></td><td class=\"link truncate\"><div>%s</div></td><td class=\"destination truncate\"><div>%s</div></td><td class=\"operation\"><div>%s</div></td><td class=\"date\"><div><span>inizio: %s</span><span>fine: %s</span></div></td><td class=\"actions\"><div><a href=\"javascript:download([%s]);\"><img class=\"start\" src=\"\" border=\"0\" /></a><a href=\"javascript:stopdownload([%s]);\"><img class=\"stop\" src=\"\" border=\"0\" /></a><a href=\"javascript:removedownload([%s]);\"><img class=\"remove\" src=\"\" border=\"0\" /></a></div></td>", $record["id"], $record["id"], $record["filename"], (! empty($record["new_filename"]) ? "orig: ".$record["new_filename"] : ""), $record["url"], $record["destinationpath"], $record["action"], parseData($record["datestart"]), parseData($record["dateend"]), ($record["status"]==0 || $record["status"]==2 ? $record["id"] : ""), ($record["status"]==3 ? $record["id"] : ""), $record["id"]);
        $html .= "</tr>";
    }
    
    return $html;
}

function parseData($valore) {
    if (preg_match('/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/', $valore, $data)) {
        return $data[3]."/".$data[2]."/".$data[1]." ".$data[4].".".$data[5];//.".".$data[6];
    } else {
        return $valore;
    }
}

function downloadById($listid) {
    $listid_query = "";
    
    foreach ($listid as $id) {
        if ($listid_query != "") {
            $listid_query .= ", ";
        }
        
        $listid_query .= $id;
    }
    
    $query = $listid_query != "" ? "SELECT url, filename FROM files_catalog WHERE id IN (".$listid_query.")" : "";
    $response = "";
    
    if ($query) {
        $mysql = new Mysql();
        $mysql->connect();
        
        $result = $mysql->query($query);
        
        while ($record = $result->fetch_array(MYSQLI_ASSOC)) {
            if (isset($record["url"])) {
                $response .= sprintf("<div class=\"download-response\"><div class=\"filename\">%s</div><div class=\"result\">%s</div></div>", $record["filename"], add_to_amuled($record["url"]));
            }
        }
    }
    
    return $response;
}

function add_to_amuled($ed2k_link) {
    if ($ed2k_link) {
        $output = shell_exec("amulecmd -P=elettrodo1254 -c=\"Add ".$ed2k_link."\"");
    } else {
        $output = "link ed2k not setted";
    }
    
    return $output;
}

$json = isset($_POST["params"]) ? json_decode($_POST["params"]) : null;

switch ($_POST["method"]) {
    case "getrecord":
        echo buildTable(isset($json->page) ? $json->page : 1, $json->order);
        break;
    case "download":
        echo downloadById($json->id);
        break;
}
?>
